// component/index/tips/worktime.js
const app = getApp();

function formatDate(value, format) {
    return app.func.render(format, value);
}

Component({
    "options": {
        styleIsolation: 'isolated'
    },
    /**
     * 组件的属性列表
     */
    properties: {
        value: {type: Object, value: null},
        format: {type: String, value: '（工作日{{start}}-{{end}},节假日除外）'}
    },

    /**
     * 组件的初始数据
     */
    data: {
        times: []
    },

    lifetimes: {
        attached() {
            let result = [];
            for (let i = 0; i <= 24; i++) {
                if (i < 10) {
                    result.push("0" + i + ":00");
                } else {
                    result.push(i + ":00");
                }
            }
            this.setData({
                times: result
            })
        }
    },

    observers: {
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onchange: function (e) {
            var index = e.detail.value;
            var value = this.data.times[index];
            var field = e.currentTarget.dataset.field;
            this.setData({
                ["value." + field]: value
            }, () => {
                var result = formatDate(this.data.value, this.data.format);
                this.triggerEvent('input', {value: result, _v: this.data.value})
            });
        }
    }
})
